﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour,IDamageable
{
    private Rigidbody2D rb;
    private Animator anim;
    private FixedJoystick joystick;
    
    public float speed;
    public float jumpforce;

    [Header("Player State")]
    public float  Health;
    public bool isDead;

    [Header("Ground Check")]
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask groundLayer;

    [Header("Srates Check")]
    public bool Canjump;
    public bool isjump;
    public bool isGround;

    [Header("Jump FX")]
    public GameObject landFX;
    public GameObject jumpfx;

    [Header("Atteck Settings")]
    public GameObject bombPrefab;
    public float nextAttack = 0;
    public float attackRate;



    // Start is called before the first frame update
    void Start()
    {

        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        joystick = FindObjectOfType<FixedJoystick>();

        GameManager.instance.IsPlayer(this);

        Health = GameManager.instance.LoadHealth();
        UIManager.instance.UpdateHealthbar(Health);

    }

    // Update is called once per frame
    void Update()
    {
        anim.SetBool("isdead", isDead);
        if (isDead)
            return;
        CheckInput();
       
    }
    public void FixedUpdate()
    {
        if(isDead )
        {
            rb.velocity = Vector2.zero;
            return;
        }
        PhysicsCheck();
        Movement();
        Jump();
    }
    void CheckInput()
    {
        if (Input.GetButtonDown("Jump") && isGround)
        {

            Canjump = true;
        }
        if (Input.GetKeyDown(KeyCode.J))
        {
            Attack();

        }
    }

    void Movement()
    {
        //float horizontalInput = Input.GetAxisRaw("Horizontal");

        float horizontalInput = joystick.Horizontal;
        rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y);
        // if (horizontalInput != 0)  
        //   transform.localScale = new Vector3(horizontalInput, 1, 1);
        if (horizontalInput > 0)
            transform.eulerAngles = new Vector3(0, 0, 0);
        if (horizontalInput <0)
            transform.eulerAngles = new Vector3(0, 180, 0);
    }



    public void Jump()
    {
        if (isjump) landFX.SetActive(false);
        if (Canjump)
        {

            isjump = true;
            jumpfx.SetActive(true);
            jumpfx.transform.position = transform.position + new Vector3(0, -0.5f, 0);
            rb.velocity = new Vector2(rb.velocity.x, jumpforce);
            rb.gravityScale = 5;
            Canjump = false;
        }

    }
    public void ButtonJump()
    {
        if(isGround )
        Canjump = true;
    }
    public void Attack()
    {
        if (Time.time > nextAttack)
        {
            Instantiate(bombPrefab, transform.position, bombPrefab.transform.rotation);

            nextAttack = Time.time + attackRate;
        }
    }
    void PhysicsCheck()
    {
        isGround = Physics2D.OverlapCircle(groundCheck.position, checkRadius, groundLayer);
        if (isGround)
        {
            rb.gravityScale = 5;
            isjump = false;

        }
    }
    public void landfx()
    {
        landFX.SetActive(true);
        landFX.transform.position = transform.position + new Vector3(0, -0.75f, 0);
    }

    public void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(groundCheck.position, checkRadius);
    }

    public void Gethit(float damage)
    {
        if (!anim.GetCurrentAnimatorStateInfo(1).IsName("Hit"))
        {
            Health -= damage;
            if (Health < 1)
            {
                Health = 0;
                isDead = true;
            }
            anim.SetTrigger("hit");

            UIManager.instance.UpdateHealthbar(Health);


        }

    }
}